<template>
  <div>
    <template v-for="(item, index) in options" :key="index">
      <a-space align="center" direction="vertical" fill style="margin-bottom: 1px">
        <template v-if="values.includes(item.value)">
          <span
            v-if="
              (item.ArcoTagType == 'default' || item.ArcoTagType == '') && (item.arcoTagClass == '' || item.arcoTagClass == null)
            "
            :key="item.value"
            :class="item.arcoTagClass"
          >
            {{ item.label + " " }}
          </span>
          <a-tag v-else :key="item.value + ''" :color="item.ArcoTagType" :index="index">
            {{ item.label + " " }}
          </a-tag>
        </template>
      </a-space>
    </template>
    <template v-if="unMatch && showValue">
      {{ unMatchArray }}
    </template>
  </div>
</template>

<script lang="ts" setup>
import { propTypes } from "@/utils/propTypes";

const props = defineProps({
  // 数据
  options: {
    type: Array as any,
    default: null
  },
  // 当前的值
  value: [Number, String, Array] as any,
  // 当未找到匹配的数据时，显示value
  showValue: propTypes.bool.def(true),
  separator: propTypes.string.def(",")
});
const values = computed(() => {
  if (props.value == "" || props.value == null || typeof props.value == "undefined") {
    return [];
  }
  return Array.isArray(props.value) ? props.value.map(item => "" + item) : String(props.value).split(props?.separator);
});

const unMatch = computed(() => {
  if (props.options?.length == 0 || props.value == "" || props.value == null || typeof props.value == "undefined") {
    return false;
  }
  // 传入值为非数组
  values.value.forEach(item => {
    if (!props.options?.some(v => v.value == item)) {
      return true; // 如果有未匹配项，将标志设置为true
    }
  });
  return false; // 返回标志的值
});

const unMatchArray = computed(() => {
  // 记录未匹配的项
  const itemUnMatchArray: Array<string | number> = [];
  if (props.value !== "" && props.value !== null && typeof props.value !== "undefined") {
    values.value.forEach(item => {
      if (!props.options?.some(v => v.value == item)) {
        itemUnMatchArray.push(item);
      }
    });
  }
  // 没有value不显示
  return handleArray(itemUnMatchArray);
});

const handleArray = (array: Array<string | number>) => {
  if (array.length == 0) return "";
  return array.reduce((pre, cur) => {
    return pre + " " + cur;
  });
};
</script>

<style scoped>
.arco-tag + .arco-tag {
  margin-bottom: 10px;
}
</style>
